/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/10
 * Time: 17:17
 * Description:
 */
package com.hangkai.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.dao.SetmealDao;
import com.hangkai.reggie.domain.Category;
import com.hangkai.reggie.domain.Setmeal;
import com.hangkai.reggie.domain.SetmealDish;
import com.hangkai.reggie.dto.SetmealDto;
import com.hangkai.reggie.service.CategoryService;
import com.hangkai.reggie.service.SetmealDishService;
import com.hangkai.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 保存套餐 同时要保存setmealDish表的数据 保存了菜品关联的套餐id
     * @param setmealDto
     */
    @Transactional
    public void saveSetmealWithDishes(SetmealDto setmealDto) {
        this.save(setmealDto);
        //获得套餐id
        Long setmealId = setmealDto.getId();
        //获得套餐里的菜品
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes = dishes.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息
        setmealDishService.saveBatch(dishes);
    }

    /**
     * 更新套餐 同时要保存setmealDish表的数据 保存了菜品关联的套餐id
     * @param setmealDto
     */
    @Transactional
    public void updateWithDishes(SetmealDto setmealDto) {
        this.updateById(setmealDto);//根据id  他自己匹配属性就赋值了
        //获得套餐id
        Long setmealId = setmealDto.getId();
        //条件构造器
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealId);
        //先从菜品套餐关联表里删除当前套餐的菜品
        setmealDishService.remove(lqw);
        //得到菜品表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //给菜品赋上套餐的id值
        for(SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 分页查询 同时要查出cateGory的name属性
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> getSetmealPage(int page, int pageSize, String name) {
        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> pageInfo2 = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        //按修改时间排序
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //like 模糊查询
        lqw.like(null != name, Setmeal::getName, name);
        //查到的数据存在pageInfo里
        this.page(pageInfo, lqw);
        //拷贝分页信息 不包括records属性
        BeanUtils.copyProperties(pageInfo, pageInfo2, "records");
        //得到套餐信息
        List<Setmeal> records = pageInfo.getRecords();
        //用得到的categoryId查到的category name 赋值给setmealDto的categoryName属性
        List<SetmealDto> setmealDtos = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category categoryEntity = categoryService.getById(categoryId);
            //将查到的categoryName存到setmealDtos里
            if (categoryEntity != null) {
                setmealDto.setCategoryName(categoryEntity.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        //set records
        pageInfo2.setRecords(setmealDtos);
        return pageInfo2;
    }

    /**
     * 批量删除套餐 注意要同时删除关联表setmealDish里的数据
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //先从setmeal表里删除数据
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //再上setmealDish里一个个删除关联的菜品套餐
        lqw.in(SetmealDish::getSetmealId, ids);
        //删除关系表里的数据
        setmealDishService.remove(lqw);
    }

    /**
     * 查询获得setmeal数据 并带上Dish 存在setmealDto里
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getSetmealWithDish(Long id) {
        Setmeal setmeal = this.getById(id);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }
}
